﻿using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using Microsoft.Web.Preview.Services;
using BLL.WWW_WWW.Group;
using Model.WWW_WWW.Group;
using BLF.WWW_WWW.Group;
using Model;
using Lx.Web.Common.UI;
using Lx.Web.Common.ControlsUI;

namespace WebServices.WWW
{
    [WebService(Namespace = "http://www.italki.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class Groups : System.Web.Services.WebService
    {        
        /// <summary>
        /// 添加讨论话题
        /// </summary>
        /// <param name="gid">对应的组ID</param>
        /// <param name="title">标题</param>
        /// <param name="desc">内容</param>
        /// <param name="tags">标签</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AddTopic(string gid, string title, string desc)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            TopicModel model = new TopicModel();
            model.GID = Int64.Parse(gid);
            model.Title = title;
            model.Desc = desc;
            model.Memberno = memberno;
            return BLF.WWW_WWW.Group.Group.AddTopic(model);
        }
        /// <summary>
        /// 获取邀请成员信息（0：当前会员好友数量、1：朋友信息（号、昵称）、2：当前会员昵称名、3:文字相关、4：分页、5：我的编号）
        /// </summary>
        /// <param name="memberno">当前会员号</param>
        /// <param name="pageIndex">pageIndex</param>
        /// <param name="pageSize">pageSize</param>
        /// <returns>ArrayList（0：所有朋友（号、昵称）、1：当前会员昵称名）</returns>
        [WebMethod]
        public ArrayList GetInviteInfo(int pageIndex, int pageSize)
        {
            ArrayList outList;
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                outList = new ArrayList();
                outList.Add("NL");
                return outList;
            }

            outList = BLF.WWW_WWW.Contact.Contacts.GetShareResInfo(memberno, pageIndex, pageSize);
            if (outList == null)
            {
                outList = new ArrayList();
                outList.Add("F");
                return outList;
            }
            string[] sText = new string[] { 
                Lx.Web.Common.Resource.GetText(Globals.Language, "C0047"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "C0056"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "C0064"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR000"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR001"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR002"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR003"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "GP030"),//SR005
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR009"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR010"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR014"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "SR017"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "GP218"),
                Lx.Web.Common.Resource.GetText(Globals.Language, "GP029")//SR016
            };
            outList.Add(sText);
            outList.Add(new BLF.PageableObject((int)outList[0], pageIndex, pageSize));
            outList.Add(memberno);
            return outList;
        }
        /// <summary>
        /// 获取会员好友列表(Nickname,MemberNo)
        /// </summary>
        /// <param name="memberno">当前会员号</param>
        /// <param name="pageIndex">pageIndex</param>
        /// <param name="pageSize">pageSize</param>        
        /// <returns>ArrayList（0好友列表 1PageableObject）</returns>
        [WebMethod]
        public ArrayList GetMemberFriends(string memberno, int pageIndex, int pageSize)
        {
            return BLF.WWW_WWW.Contact.Contacts.GetMemberFriends(memberno, pageIndex, pageSize);
        }
        /// <summary>
        /// 发送邮件(GroupInvitation.xml)
        /// </summary>
        /// <param name="fromName">发送者昵称名</param>
        /// <param name="fEmails">被发送者email</param>
        /// <param name="shareContent">共享内容</param>
        /// <param name="SharedURL">共享url</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage SendMail(string fromName, string fEmails, string SharedURL,string title)
        {
            CommonMessage comMes = new CommonMessage();
            string myMemberno = HttpContext.Current.User.Identity.Name;
            if (myMemberno == "" || SharedURL == "")
            {
                return comMes;
            }
            
            BaseControl baseControl = new BaseControl();
            string eSubject = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupInvitation.xml", Globals.Language))), "email", "subject", "");
            string eBody = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupInvitation.xml", Globals.Language))), "email", "body", "");
            string[] emails = fEmails.Split(',');
            new System.Threading.Thread(new System.Threading.ThreadStart(delegate
            {
                for (int i = 0; i < emails.Length; i++)
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(emails[i], "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
                    {
                        eSubject = eSubject.Replace("{0}", fromName).Replace("{1}", title);
                        eBody = eBody.Replace("{0}", fromName).Replace("{1}", title).Replace("{2}", SharedURL);
                        MsMailSend.SendMail(eSubject, eBody, emails[i], null, true);
                    }
                }
            })).Start();
            return comMes;
        }
        /// <summary>
        /// 发送站内信息(GroupInvitei.xml)
        /// </summary>
        /// <param name="fromName">发送者昵称名</param>
        /// <param name="friendsMn">被发送者email列表</param>
        /// <param name="msg">信息</param>
        /// <param name="SharedURL">共享url</param>
        /// <param name="CheckAOrF">是否全选标记（true表示全选、如果friendsMn不为空表示不选择这些人）</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage SendItalkiMail(string fromName, string friendsMns, string SharedURL, bool checkAOrF,string title)
        {
            CommonMessage comMes = new CommonMessage();
            string myMemberno = HttpContext.Current.User.Identity.Name;
            if (myMemberno == "" || SharedURL == "" || (friendsMns == "" && !checkAOrF))
            {
                return comMes;
            }            
            BaseControl baseControl = new BaseControl();
            string eSubject = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupInvitei.xml", Globals.Language))), "email", "subject", "");
            string eBody = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupInvitei.xml", Globals.Language))), "email", "body", "");
            eSubject = eSubject.Replace("{0}", title);
            eBody = eBody.Replace("{0}", fromName).Replace("{1}", title).Replace("{2}", SharedURL).Replace("{3}", myMemberno);

            BLF.WWW_WWW.Contact.Contacts.ShareResSendMail(myMemberno, friendsMns, eSubject, eBody, checkAOrF);
            return comMes;
        }
        /// <summary>
        /// 申请加入组
        /// </summary>
        /// <param name="gid">组ID</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage Join1Group(string gid)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Group.Group.JoinGroup(gid, memberno);
        }
        /// <summary>
        /// 离开组
        /// </summary>
        /// <param name="gid">组ID</param>
        /// <returns>CommonMessage</returns>
        public CommonMessage LeaveGroup(string gid)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Group.Group.LeaveGroup(gid, memberno);
        }
        /// <summary>
        /// 申请管理员
        /// </summary>
        /// <param name="gid">组ID</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage AppAdmin1(string gid)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Group.Group.AppAdmin(gid, memberno);
        }
        /// <summary>
        /// 删除讨论话题
        /// </summary>
        /// <param name="tid">讨论话题ID</param>        
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage DeleteTopic(string tid)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            return BLF.WWW_WWW.Group.Group.DeleteTopic(tid, memberno);
        }
        /// <summary>
        /// 删除帖子回复并重新获取数据（0：分页对象、1：讨论评论列表TopicReplyModel List）
        /// </summary>
        /// <param name="tid">帖子ID</param>
        /// <param name="pageIndex">pageIndex</param>
        /// <param name="pageSize">pageSize</param>
        /// <returns>0：记录数int、1：1：讨论评论列表TopicReplyModel List</returns>
        [WebMethod]
        public ArrayList DelReplyTopic(string tid, int pageIndex, int pageSize)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new ArrayList();
            }
            ArrayList arrList = BLF.WWW_WWW.Group.Group.DelReplyTopic(tid, memberno, pageIndex, pageSize);            
            int rCount = (int)arrList[0];
            int pCount = (int)Math.Ceiling(((decimal)rCount) / ((decimal)pageSize));
            if (pageIndex > pCount) pageIndex = pCount;

            BLF.PageableObject pObj = new BLF.PageableObject(rCount, pageIndex, pageSize);            
            List<TopicReplyModel> cList = arrList[1] as List<TopicReplyModel>;
            BLF.WWW_WWW.Group.Group.ParentContent(cList, arrList[2] as List<TopicReplyModel>);
            arrList = new ArrayList();
            arrList.Add(pObj);
            arrList.Add(cList);            
            return arrList;
        }
        /// <summary>
        /// 回复帖子并重新获取数据（0：记录数int、1：1：讨论评论列表TopicReplyModel List、2：讨论评论列表父内容TopicReplyModel List）
        /// </summary>        
        /// <returns>0：记录数int、1：1：讨论评论列表TopicReplyModel List、2：讨论评论列表父内容TopicReplyModel List</returns>
        [WebMethod]
        public ArrayList ReplyTopic(string fid, string pid, string topic, int pageSize)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new ArrayList();
            }
            TopicReplyModel model = new TopicReplyModel();
            model.FID = Int64.Parse(fid);
            model.ParentID = string.IsNullOrEmpty(pid) ? Guid.Empty : new Guid(pid);
            model.Memberno = memberno;
            model.Topic = topic;
            ArrayList arrList = BLF.WWW_WWW.Group.Group.ReplyTopic(model, pageSize);
            int rCount = (int)arrList[0];
            int pageIndex = (int)Math.Ceiling(((decimal)rCount) / ((decimal)pageSize));            

            BLF.PageableObject pObj = new BLF.PageableObject(rCount, pageIndex, pageSize);
            List<TopicReplyModel> cList = arrList[1] as List<TopicReplyModel>;
            BLF.WWW_WWW.Group.Group.ParentContent(cList, arrList[2] as List<TopicReplyModel>);
            arrList = new ArrayList();
            arrList.Add(pObj);
            arrList.Add(cList);
            return arrList;
        }
        /// <summary>
        /// 申请加入组
        /// </summary>
        /// <param name="gid">组ID</param>
        /// <param name="nickName">当前会员昵称</param>
        /// <param name="gTitle">组标题</param>
        /// <returns>CommonMessage</returns>
        [WebMethod]
        public CommonMessage JoinGroup(string gid, string nickName, string gTitle)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            BaseControl baseControl = new BaseControl();
            string eSubject = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupJoin.xml", Globals.Language))), "email", "subject", "");
            string eBody = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupJoin.xml", Globals.Language))), "email", "body", "");
            eSubject = eSubject.Replace("{0}", gTitle);
            eBody = eBody.Replace("{0}", gTitle).Replace("{1}", gid).Replace("{2}", nickName).Replace("{3}", memberno);
            return BLF.WWW_WWW.Group.Group.JoinGroup(gid, memberno, eSubject, eBody);
        }
        /// <summary>
        /// 申请管理员并发站内信
        /// </summary>
        /// <param name="gid">组ID</param>
        /// <param name="nickName">当前会员昵称</param>
        /// <param name="gTitle">组标题</param>
        /// <param name="message">消息</param>
        /// <returns></returns>
        [WebMethod]
        public CommonMessage AppAdmin(string gid,string nickName,string gTitle,string message)
        {
            string memberno = HttpContext.Current.User.Identity.Name;
            if (memberno == "")
            {
                return new CommonMessage();
            }
            BaseControl baseControl = new BaseControl();
            string eSubject = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupAppAdmin.xml", Globals.Language))), "email", "subject", "");
            string eBody = Globals.ReadTextByXmlNode(baseControl.FormatFileName(Server.MapPath(String.Format("/Template/emailxml/{0}/GroupAppAdmin.xml", Globals.Language))), "email", "body", "");
            eSubject = eSubject.Replace("{0}", gTitle);
            eBody = eBody.Replace("{0}", gTitle).Replace("{1}", gid).Replace("{2}", nickName).Replace("{3}", memberno).Replace("{4}", message);
            return BLF.WWW_WWW.Group.Group.AppAdmin(gid, memberno, eSubject, eBody);
        }
    }
}
